<?php

namespace App\Validators\Teacher;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class TeacherValidator extends LaravelValidator{

    use PassesValidator;
    protected $rulesMessages=[
        'id_card.required' => '身份证号码是必填',
        'id_card.required' => '身份证号码是重复',
    ];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'id_card' => 'required|phone|unique:teacher',
            'acquire_office_date'=>'required',
            'old_name'=>'required',
            'id_card'=>'required',
            'nation'=>'required',
            'birth_date'=>'required',
            'start_work_date'=>'required',
            'hometown'=>'required',
            'graduate_date'=>'required',
            'graduate_school'=>'required',
            'graduate_major'=>'required',
            'graduate_degree'=>'required',
            'politics_status'=>'required',
            'join_politics_org_date'=>'required',
            'family_connect_user_name'=>'required',
            'family_connect_user_relation'=>'required',
            'family_connect_user_mobile'=>'required',
            'family_connect_user_company'=>'required',
            'family_connect_user_job'=>'required',
            'family_connect_address'=>'required',
        ],
        ValidatorInterface::RULE_UPDATE => [
            'id_card' => 'required|phone|unique:teacher',
            'id_card' => 'required|unique:teacher,id_card,{id}',
            'acquire_office_date'=>'required',
            'old_name'=>'required',
            'id_card'=>'required',
            'nation'=>'required',
            'birth_date'=>'required',
            'start_work_date'=>'required',
            'hometown'=>'required',
            'graduate_date'=>'required',
            'graduate_school'=>'required',
            'graduate_major'=>'required',
            'graduate_degree'=>'required',
            'politics_status'=>'required',
            'join_politics_org_date'=>'required',
            'family_connect_user_name'=>'required',
            'family_connect_user_relation'=>'required',
            'family_connect_user_mobile'=>'required',
            'family_connect_user_company'=>'required',
            'family_connect_user_job'=>'required',
            'family_connect_address'=>'required',
        ],
   ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
